package com.cgwx.systemservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgwx.servicebase.exceptionhandler.MyException;
import com.cgwx.systemservice.entity.sys.SysMenu;
import com.cgwx.systemservice.entity.vo.RouterVo;
import com.cgwx.systemservice.mapper.SysMenuMapper;
import com.cgwx.systemservice.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cgwx.systemservice.utils.MenuHelper;
import com.cgwx.systemservice.utils.RouterHelper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author GaoWei
 * @since 2023-10-12
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

//    查询所有菜单
    @Override
    public List<SysMenu> findNodes() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort_value");
//        获取所有菜单
        List<SysMenu> sysMenuList = baseMapper.selectList(queryWrapper);
//        把所有菜单数据转换为要求格式
        return MenuHelper.buildTree(sysMenuList);
    }

//    删除菜单方法
    @Override
    public void removeMenuById(String id) {
//        查询当前节点是否有子节点
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        if (selectCount > 0){
            throw new MyException(201,"请先删除子菜单");
        }
        baseMapper.deleteById(id);
    }

//    根据用户id查询菜单权限
    @Override
    public List<RouterVo> getUserMenuList(Long userId) {
        List<SysMenu> sysMenuList;
//        admin是超级管理员，可以操作所有内容，userId 固定为1
        if(userId == 1){
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);
            queryWrapper.orderByAsc("sort_value");
            sysMenuList = baseMapper.selectList(queryWrapper);
        }else{
//        其他用户查询，当前用户的权限
            sysMenuList = baseMapper.findMenuListByUserId(userId);
        }
//        构建树形结构
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
//        转换为前端路由要求格式数据并返回
        return RouterHelper.buildRouters(sysMenuTreeList);
    }

//    根据用户id查询按钮权限
    @Override
    public List<String> getUserButtonList(Long userId) {
        List<SysMenu> sysMenuList;
//        admin是超级管理员，可以操作所有内容，userId 固定为1
        if(userId == 1){
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);
            queryWrapper.orderByAsc("sort_value");
            sysMenuList = baseMapper.selectList(queryWrapper);
        }else{
//        其他用户查询，当前用户的权限
            sysMenuList = baseMapper.findMenuListByUserId(userId);
        }
//        遍历菜单集合
        List<String> permissionList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
//            type = 2
            if(sysMenu.getType() == 2){
                permissionList.add(sysMenu.getPerms());
            }
        }
        return permissionList;
    }
}
